#!/usr/sbin/rsct/perl5/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2002 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)28   1.7   src/rsct/rm/SensorRM/cli/bin/lssensor.perl, sensorcli, rsct_rpyxh, rpyxht1f3 5/20/02 11:00:45

# Lists resources from the RMC IBM.Sensor class

use strict;
use locale;
BEGIN
  {
	# this enables us to redirect where it looks for other RSCT files during development
	$::rsctroot = $ENV{'RSCT_ROOT'} || '/usr/sbin/rsct';
	$::rsctpm = "$::rsctroot/pm";
	$::rsctmsgmaps = "$::rsctroot/msgmaps";
  }

use lib $::rsctpm;
use Getopt::Std;
use autouse CT_cli_utils => qw(printIMsg printEMsg);
use Socket;

$main::PROGNAME = 'lssensor';
$main::MSGCAT = 'sensorcli.cat';
#$ENV{MSGMAPPATH} = $::rsctmsgmaps;     # this does not do anything because printIMsg overrides it
$main::LSMSG = '/usr/sbin/rsct/bin/ctdspmsg';
#$main::Trace = 1;

# For the usage, see sensorcli.msg
sub usage { printIMsg('IMsgLssensorUsage');  exit (scalar(@_) ? $_[0] : 1); }


# Parse the cmd line args and check them
if (! getopts('Aan:hvV') ) { &usage; }
if ((scalar(@ARGV)>0 && $::opt_A) || $::opt_h) { &usage; }
if ($::opt_a && $::opt_n) { &usage; }
if ($::opt_V) { $::opt_v = 1; }

my $options = '';
my $selectstr = '';
my $attr = '';
my $longformat = 0;
if ($::opt_A)       # show everything
  {
	$options = "-D ':|:' -n -i";
	$attr = "'*b0x0020'";
	$longformat = 1;
  }
elsif (scalar(@ARGV) >= 1)       # show all attributes of this sensor
  {
	$options = "-D ':|:' -n";
	$attr = "'*b0x0020'";
	$selectstr = q/Name IN ('/ . join("','",@ARGV) . q/')/;
	$longformat = 1;
  }
else           # show all sensor names
  {
	$options = "-i -D ':|:'";
	$attr = 'Name';
	if ($::opt_a || defined($::opt_n)) { $attr .= '::NodeNameList'; }
  }

if ($::opt_a || defined($::opt_n))
  {
	#todo: support symbolic names for values 2 and 3
	if ($ENV{CT_MANAGEMENT_SCOPE}!=2 && $ENV{CT_MANAGEMENT_SCOPE}!=3) { $ENV{CT_MANAGEMENT_SCOPE}=4; }
  }
else { $ENV{CT_MANAGEMENT_SCOPE}=1; }

if (defined($::opt_n))
  {
	my @nodes = split(/[, ]+/,$::opt_n);
	#&resolve(\@nodes);
	if (length($selectstr)) { $selectstr .= ' && '; }
	$selectstr .= q/NodeNameList IN ('/ . join("','",@nodes) . q/')/;
  }

if (length($selectstr)) { $selectstr = qq("$selectstr"); }

my $cmd = qq(/usr/bin/lsrsrc-api $options -s IBM.Sensor::${selectstr}::$attr 2>&1);
if ($::opt_v) { printIMsg('IMsgRmcCmd', $cmd); }

my @output = `$cmd`;
my $rc = $? >> 8;
if ($rc)
  {
	#print $output[0], "\n";
	if (!$::opt_v) { $output[0] =~ s/.*:\|:.*:\|:.*:\|:.*:\|:.*:\|://; }
	print @output;
	exit $rc;
  }
chop(@output);
if ($longformat) { &displayLong; }
else
  {
	foreach my $l (@output)
	  {
		my ($name, $node) = split(/:\|:/, $l);
		if (length($node)) { $node = "   $node"; }
		print $name, $node, "\n";
	  }
  }
#if (!scalar(@output)) { print "\n"; }

exit;


sub displayLong
  {
	my $firsttime = 1;
	foreach my $l (@output)
	  {
		if (!$firsttime) { print "-------------------\n"; }   # separate this resource from the previous
		else { $firsttime = 0; }

		# takes the alternating attrs and values and puts them in the hash correctly
		my %attrs = split(/:\|:/, $l);

		# Make sure the hostname is displayed 1st
		if (defined($attrs{Name}))
		  {
			print " Name = $attrs{Name}\n";
			delete $attrs{Name};          # so we do not repeat it in the following loop
		  }

		# Now display the rest of the attributes, sorted
		foreach my $k (sort keys %attrs)
		  {	print " $k = $attrs{$k}\n"; }
	  }
  }


# Not used right now
sub resolve
  {
	my ($arrayref) = @_;
	foreach my $host (@$arrayref)
	  {
		if ($host =~ /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/)     # an ip address
		  {
			my $packedaddr = inet_aton($host);
			my $hostname = gethostbyaddr($packedaddr, AF_INET);
			if (length($hostname)) { $host = $hostname; }
		  }
		else					# they specified a hostname, but it may be a short name
		  {
			my ($hostname, $aliases, $addrtype, $length, @addrs) = gethostbyname($host);
			if (length($hostname)) { $host = $hostname; }
		  }
	  }
  }


# Not used right now
sub isHACluster
  {
	my $output = `/usr/sbin/rsct/bin/ct_clusterinfo -c`;
	chomp $output;
	if ($? || !length($output) || $output=~/^IW/) { return 0; }
	else { return 1; }
  }
